from tkinter import *
from tkinter.filedialog import askdirectory
from tkinter.filedialog import *
import cv2
import numpy as np

def cv_imread(file_path):
    return cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),-1)
def selectPath():
    path_ = askopenfilename(filetypes=[("jpg","png")])
    while True:
        if path_.endswith("jpg") or path_.endswith('png'):
            break
        path_ = askopenfilename(filetypes=[("jpg","png")])
    print(path_)
    close()
    draw(path_)
def draw_with_rec(event,x,y,flags,param):
    global im, point1, point2
    global origin_img
    img2 = origin_img.copy()
    if event == cv2.EVENT_LBUTTONDOWN:         #左键点击
        point1 = (x,y)
    elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON):               #按住左键拖曳
        cv2.rectangle(img2, point1, (x,y), (255,0,0), 5)
        cv2.imshow('image', img2)
    elif event == cv2.EVENT_LBUTTONUP:         #左键释放
        point2 = (x,y)
        cv2.rectangle(img2, point1, point2, (0,0,255), 5)
        cv2.imshow('image', img2)
        min_x = min(point1[0],point2[0])
        min_y = min(point1[1],point2[1])
        max_x = max(point1[0],point2[0])
        max_y = max(point1[1],point2[1])
        # width = abs(point1[0] - point2[0])
        # height = abs(point1[1] -point2[1])
        # global mouseState
        # mouseState = 0
        print(min_x,min_y,max_x,max_y)
        # cv2.rectangle(img2, (min_x,min_y), (max_x,max_y), (0,255,255), 50)
        cv2.imshow('image', img2)

        size = 10
        # drawMask(min_y,min_x,size*20)
        temp_y = min_y
        im = origin_img
        while min_x < max_x + size:
            while min_y < max_y + size:
                drawMask(min_y,min_x,size)
                min_y += size
            min_y = temp_y
            min_x += size

        cv2.imshow('image', im)
        
        origin_img = im
    im = img2
    return 0
def onMouse(event, x, y, flags, param):
    # 鼠标左键按下并且移动
    if mouseState != 0:
        return draw_with_rec(event,x,y,flags,param)
    if event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON:
        # 调用函数打马赛克
        if usemask:
            drawMask(y, x)
def drawMask(x,y,size=10):
    global im
    m = x // size*size
    n = y // size*size
    print("m,n is:",m,n)
    for i in range(size):
        for j in range(size):
            im[m + i][n + j] = im[m][n]
def use_mask(state):
    global usemask
    if state == 0:
        usemask = False
    else:
        usemask = True
def use_rect(state):
    global mouseState
    if state == 0:
        mouseState = 0
    else:
        mouseState = 1
def draw(img_path):
    global im
    global usemask
    global mouseState
    global origin_img
    im = cv_imread(img_path)
    origin_img = im
    usemask = False
    mouseState = 0
    print(im.shape)
    # 新建立窗口
    width=1200
    height=800
    cv2.namedWindow('image')
    cv2.resizeWindow('image',640,480)
    cv2.setMouseCallback('image', onMouse)
    cv2.createTrackbar("use mask","image",0,1,use_mask)
    cv2.createTrackbar("use rect","image",0,3,use_rect)

    cv2.setWindowProperty("image", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

    while 1:
        cv2.imshow('image', im)
        # 按ESC键退出
        if cv2.waitKey(10) & 0xFF == 27:
            break
    cv2.destroyAllWindows()
    pass
def close():
    root.destroy()


if __name__ == "__main__":
    root = Tk()
    path = StringVar()

    Label(root,text = "目标文件:").grid(row = 0, column = 0)
    Entry(root, textvariable = path).grid(row = 0, column = 1)
    Button(root, text = "文件选择", command = selectPath).grid(row = 0, column = 2)
    Button(root, text = "关闭窗口", command = close).grid(row = 1, column = 0)
    root.mainloop()